package com.bosssoft.boss_exam_core.common.config.StorageConfig;


import com.bosssoft.boss_exam_core.storage.LocalStorage;
import com.bosssoft.boss_exam_core.storage.QiniuStorage;
import com.bosssoft.boss_exam_core.storage.StorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@EnableConfigurationProperties
@Configuration
public class StorageAutoConfiguration {

    @Autowired
    private StorageProperties properties;

    @Bean
    public StorageService storageService() {
        StorageService storageService = new StorageService();
        String active = this.properties.getActive();
        storageService.setActive(active);
        if(active.equals("local")){
            storageService.setStorage(localStorage());
        } else if(active.equals("qiniu")){
            storageService.setStorage(qiniuStorage());
        } else{
            throw  new RuntimeException("当前存储模式 " + active + " 不支持");
        }
        return storageService;
    }

    @Bean
    public LocalStorage localStorage() {
        LocalStorage localStorage = new LocalStorage();
        LocalProperties localProperties = this.properties.getLocalProperties();
        localStorage.setAddress(localProperties.getAddress());
        localStorage.setStoragePath(localProperties.getStoragePath());
        return localStorage;
    }
    @Bean
    public QiniuStorage qiniuStorage() {
        QiniuStorage qiniuStorage = new QiniuStorage();
        QiniuProperties qiniuProperties = this.properties.getQiniuProperties();
        qiniuStorage.setAccessKey(qiniuProperties.getAccessKey());
        qiniuStorage.setSecretKey(qiniuProperties.getSecretKey());
        qiniuStorage.setBucketName(qiniuProperties.getBucketName());
        qiniuStorage.setEndpoint(qiniuProperties.getEndpoint());
        return qiniuStorage;
    }

}
